package cn.venny.doudian.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;

/**
 * @program: pdd_cloud_print
 * @description:
 * @author: wudq
 * @create: 2020-09-30 09:58
 **/
public class StringUtils extends org.springframework.util.StringUtils {

    private static final String[] EMPTY_STRING_ARRAY = {};

    private static final String FOLDER_SEPARATOR = "/";

    private static final String WINDOWS_FOLDER_SEPARATOR = "\\";

    private static final String TOP_PATH = "..";

    private static final String CURRENT_PATH = ".";

    private static final char EXTENSION_SEPARATOR = '.';

    public static boolean strContainArray(String str, HashSet<String> arr) {
        if (arr == null) {
            throw new RuntimeException("arr can not be null");
        }
        return arr.stream().anyMatch(a -> str.contains(a));
    }

    public static boolean strContainArray(String str, List<String> arr) {
        if (arr == null) {
            throw new RuntimeException("arr can not be null");
        }
        return arr.stream().anyMatch(a -> str.contains(a));
    }

    public static boolean strContainArray(String str, String... arr) {
        if (arr == null) {
            throw new RuntimeException("arr can not be null");
        }
        for (String a : arr) {
            if (str.contains(a)) {
                return true;
            }
        }
        return false;
    }

    public static boolean notEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 取交集（交集含两个以上）
     */
    public static String intersect2(String str1, String str2) {
        List<String> result = new ArrayList<>();
        int length1 = str1.length();
        int length2 = str2.length();
        String bigStr = length1 > length2 ? str1 : str2;
        String smallStr = length1 > length2 ? str2 : str1;
        int bitLength = bigStr.length();
        int smallLength = smallStr.length();
        for (int i = 0; i < bitLength; i++) {
            for (int j = 0; j < smallLength; j++) {
                String char1 = bigStr.charAt(i) + "";
                String char2 = smallStr.charAt(j) + "";
                if (char1.equals(char2)) {
                    result.add(char1);
                }
            }
        }
        Optional<String> op = result.parallelStream().reduce(String::concat);
        return op.isPresent() && op.get().length() >= 2 ? op.get() : "";
    }


    public static String nonNullConcat(String... args) {
        StringBuilder str = new StringBuilder("");
        if (args == null || args.length == 0) {
            return str.toString();
        }
        for (String arg : args) {
            if (arg == null) {
                continue;
            }
            str.append(arg);
        }
        return str.toString();
    }

    /**
     * 设置高亮
     */
    public static String setHeighlight(String paragraph, String keyword) {
        int startIdx = paragraph.indexOf(keyword);
        if (startIdx > -1) {
            int endIdx = startIdx + keyword.length();
            return paragraph.replace(keyword, "<em style=\"color:red;\">" + keyword + "</em>");
        }
        return paragraph;
    }

    /**
     * 设置高亮
     */
    public static String setHeighlight(String paragraph, List<String> keywords) {
        for (String keyword : keywords) {
            paragraph = setHeighlight(paragraph, keyword);
        }
        return paragraph;
    }

}
